package com.example.wc.logindemo;

import android.app.Dialog;
import android.app.DialogFragment;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;

import java.util.List;

/**
 * Created by hui on 2017/3/16.
 */

public class DatePickerFragment extends DialogFragment{
    private LinearLayout mLinearLayout;
    private List<Blog> mBlogs;
    private Context mContext;
    private Blog mBlog;
    private DialogBlogGetListener mDialogBlogGetListener;

    public DatePickerFragment(Context context, List<Blog> blogs, DialogBlogGetListener dialogBlogGetListener){
        mContext = context;
        mBlogs = blogs;
        mDialogBlogGetListener = dialogBlogGetListener;
    }

    public Blog getBlog() {
        return mBlog;
    }

    public interface DialogBlogGetListener{
        public void onDialogBlogGeted(Blog blog);
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState){
//        View v = LayoutInflater.from(getActivity()).inflate(android.R.layout.simple_list_item_1, null);
//        mLinearLayout.setLayoutParams(new LinearLayout.LayoutParams(
//                LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));

        ListView baseList = new ListView(mContext);
        baseList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                mBlog = mBlogs.get(position);
                Log.d("onclickblog", getBlog().toString());
                if(mDialogBlogGetListener != null){
                    mDialogBlogGetListener.onDialogBlogGeted(mBlog);
                }
                dismiss();

            }
        });
        baseList.setAdapter(new BlogBaseAdapter(mContext, mBlogs));


//        mLinearLayout.addView(baseList);

        return new AlertDialog.Builder(getActivity()).
                setTitle(R.string.history_button_edit_blog_activity).
                setView(baseList).
                setPositiveButton(android.R.string.cancel, null).
                create();
    }
}
